package backtrack;

import java.util.ArrayList;
import java.util.List;

public class Solution78 {

    List<Integer> res = new ArrayList<>();

    List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> subsets(int[] nums) {
        boolean[] visited = new boolean[nums.length];
        backTrack(nums,0,visited);
        return result;
    }

    public void backTrack(int [] nums,int start,boolean[] visited){

        result.add(new ArrayList<>(res));

        if (start>= nums.length){
            return;
        }

        for (int i = start; i < nums.length; i++) {
            //做出选择
            if (!visited[i]){
                res.add(nums[i]);
                visited[i] = true;
            }

            backTrack(nums,i+1,visited);

            res.remove(Integer.valueOf(nums[i]));
            visited[i] = false;
        }
    }

    public static void main(String[] args) {
        Solution78 solution78 = new Solution78();
        int [] nums = new int[] {1,2,3};
        List<List<Integer>> subsets = solution78.subsets(nums);

        System.out.println("subsets = " + subsets);
    }
}
